package com.fsbm.ctech.maxis.ebpsbackend;

import java.util.*;
import java.text.*;
import com.checkfree.isolutions.customer.persist.*;
import com.checkfree.isolutions.statement.persist.*;
import com.checkfree.isolutions.utility.*;
import com.checkfree.isolutions.streaming.IClassId;
import com.checkfree.isolutions.admin.ObjectId;
import com.checkfree.isolutions.persist.*;

/**
 * Batch Program to extract statement ids of a given statement run ID. Note the control
 * given for the transaction size, if this is not controlled it may result into memory problems.
 * <p>
 * Maxis Backend Program - Extract statementids of statement run.
 *
 * @author Michael Ty Chan
 */

public class GetStatementRun {
    // Different types of statement runs. They should be opened in the 4.x API.
    private static final int AFP_STATEMENT_RUN      = 4033;
    private static final int XEROX_STATEMENT_RUN    = 4033;
    private static final int XMLS_STATEMENT_RUN     = 4033;
    private static final int LINEDATA_STATEMENT_RUN = 4033;


    public static void main(String[] args) {
        ArrayList vcfStatements = new ArrayList();

            try {
                vcfStatements = getStmtIds("1280");
                int vSize     = vcfStatements.size();
                for (int x=0; x < vSize; ++x) {
                        String myStr = (String) vcfStatements.get(x);
                }
            } catch (NumberFormatException e) {
                System.out.println("process name (main) - Invalid run id");
                System.exit(1);
            }
    }
    public static ArrayList getStmtIds(String runIDString) {
        ArrayList       vStatementIds   = new ArrayList();
        int             runId           = 0;
        int             transactionSize = 500;
        int[]           classIdArray    = {LINEDATA_STATEMENT_RUN,
                                            AFP_STATEMENT_RUN,
                                            XEROX_STATEMENT_RUN,
                                            XMLS_STATEMENT_RUN};
        ObjectId        statementRunId  = null;
        ObjectId        temp            = null;
        StatementRun    statementRun    = null;
        Statement       statement       = null;

        if (runIDString.length() > 0) {
            try {
                runId = Integer.parseInt(runIDString);
            } catch (NumberFormatException e) {
                System.out.println("process name (getStmtIds) - Invalid run id");
            }
        }

        try {
            PersistServer.activate();
            PersistServer.beginTransaction();
            // Find the run
            for (int i = 0; i < classIdArray.length; i++) {
                try {
                    statementRunId = new ObjectId(classIdArray[i], runId);
                    statementRun = StatementRunHomeImpl.singleton().findById(statementRunId);
                    System.out.println("=="+(statementRun.getId()).toString());

                    // If we get here, that means that we found the right classId
                    // for the given statement run, so we can exit the loop.
                    break;
                } catch (Throwable e) {
                    // Not the right class ID, just try the next one
                }
            }
            if (statementRun != null ) {
                // Get the first set of statements for the run
                Statement initStatement = null;

                Vector statementVector = StatementHomeImpl.singleton().findAllStatementIdsByStatementRun(statementRun, temp, 100);

                //System.out.println("Found statements, loading into a Vector that has a size of " + statementVector.size());
                while (!statementVector.isEmpty()) {
                    //System.out.println("Found " + statementVector.size() + " statements");
                    Iterator iter = statementVector.iterator();
                    while(iter.hasNext()){
                        temp = (ObjectId) iter.next();
                        Statement myStatement= StatementHomeImpl.singleton().findById(temp);
                        int id = myStatement.getId().getInstanceId();
                        //System.out.println("Statement id = " + id);

                        vStatementIds.add(Integer.toString(id));  // store statement Ids into an array list
                        // Do some stuff on the statement
                        // .....
                        }
                    // Commit
                    PersistServer.commitTransaction();

                    // Get the next set of statements for the statement run, if any
                    PersistServer.beginTransaction();
                    statementVector = StatementHomeImpl.singleton().findAllStatementIdsByStatementRun(statementRun, temp, transactionSize);
                }
                //System.out.println("No more statements.");
            } else {
                System.out.println("Can't find statement run with id " + runId);
            }

            PersistServer.commitTransaction();
        } catch (Throwable e) {
            e.printStackTrace();
            if (PersistServer.transactionIsActive()) {
                try {
                    PersistServer.rollbackTransaction();
                } catch (Exception ignored) {
                }
            }
        } finally {
            PersistServer.deactivate();
        }
        return vStatementIds;
    }
}